#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows;
using UTable.Input;
using UTable.Input.MultiTouch;

namespace UTable.Objects.Handlers
{
	public class GestureHandler: OneFingerHandler
	{
		public GestureHandler(FrameworkElement target, FrameworkElement relativeObject)
		{
			this.Target = target;
			this.RelativeObject = relativeObject;
		}

		protected override void OnOneFingerDown(List<FingerPoint> fingers)
		{
			pointCollection = new StylusPointCollection();
			Point p = Target.TranslatePoint(fingers[0].Position, RelativeObject);
			pointCollection.Add(new StylusPoint(p.X, p.Y));
		}

		protected override void OnOneFingerMove(List<FingerPoint> oFigners, List<FingerPoint> nFingers)
		{
			Point p = Target.TranslatePoint(nFingers[0].Position, RelativeObject);
			pointCollection.Add(new StylusPoint(p.X, p.Y));
		}

		protected override void OnOneFingerUp(List<FingerPoint> fingers)
		{
			StrokeCollection strokeCollection = new StrokeCollection { new Stroke(pointCollection) };
			GestureRecognitionResult result = GestureRecognizer.Recognize(strokeCollection).FirstOrDefault();
			if (result != null)
			{
				if (GestureDetected != null)
				{
					GestureDetected(new GestureDetectedEventArgs(Target as IObject, result.ApplicationGesture));
				}
				OnGestureDetected(new GestureDetectedEventArgs(Target as IObject, result.ApplicationGesture));
			}
		}

		public virtual void OnGestureDetected(GestureDetectedEventArgs args)
		{

		}

		StylusPointCollection pointCollection;

		public FrameworkElement RelativeObject { get; set; }

		public FrameworkElement Target { get; set; }

		GestureRecognizer gestureRecognizer;

		public GestureRecognizer GestureRecognizer
		{
			get
			{
				if (gestureRecognizer == null)
				{
					gestureRecognizer = new GestureRecognizer();
					
				}
				return gestureRecognizer;
			}
		}

		public bool IsGestureEnabled
		{
			get { return GestureRecognizer.IsRecognizerAvailable; }
		}

		public void EnableGestures(ApplicationGesture[] gestures)
		{
			if (IsGestureEnabled || gestures != null)
				gestureRecognizer.SetEnabledGestures(gestures);
		}

		public event GestureDetectedEventHandler GestureDetected;
	}

	public delegate void GestureDetectedEventHandler(GestureDetectedEventArgs args);

	public class GestureDetectedEventArgs: EventArgs
	{
		public GestureDetectedEventArgs(IObject target, ApplicationGesture gesture)
		{
			this.Target = target;
			this.Gesture = gesture;
		}
		public ApplicationGesture Gesture;

		public IObject Target;
	}

}
